﻿// 08.multiple_socket_poller.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>

#include <zmq.hpp>

int main()
{
	zmq::context_t ctx;

	zmq::socket_t receiver(ctx, zmq::socket_type::pull);
	receiver.connect("tcp://localhost:5557");

	zmq::socket_t subscriber(ctx, zmq::socket_type::sub);
	subscriber.connect("tcp://localhost:5556");
	subscriber.setsockopt(ZMQ_SUBSCRIBE, "1001", 4);

	std::vector<zmq::pollitem_t> items = {
		{ receiver, 0, ZMQ_POLLIN, 0 },
		{ subscriber, 0, ZMQ_POLLIN, 0 }
	};

	while (true)
	{
		auto tmp_item = items;

		zmq::message_t msg;
		zmq::poll(tmp_item);
		if (tmp_item[0].revents & ZMQ_POLLIN)
		{
			receiver.recv(&msg);
			std::cout << "Recv msg from puller" << std::endl;
		}
		else if(tmp_item[1].revents & ZMQ_POLLIN)
		{
			subscriber.recv(&msg);
			std::cout << "Recv msg from subsriber" << std::endl;
		}
	}
}